﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This part contains functions for the update organization
 */

using MCServeLib;
using System;
using System.Diagnostics;
using System.IO;

namespace MCServeUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            #region # Check the program environment #

            if (!Settings.CurrentPath.Contains(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)))
            {
                /**
                 * The Update Manager is not in the %appdata%-Area and must be copied there.
                 */
                // Create the MCServeUpdate Folder in %appdata%
                if(!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MCServeUpdate"))
                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MCServeUpdate");

                // Copy the update-manager and the used librariey to the appdata-folder
                File.Copy(Settings.CurrentPath + "\\MCServeUpdate.exe", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MCServeUpdate\\MCServeUpdate.exe", true);
                File.Copy(Settings.CurrentPath + "\\MCServeLib.dll", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MCServeUpdate\\MCServeLib.dll", true);

                // Start the update manager from the appdata-folder
                Process UpdateManagerProcess = new Process();
                UpdateManagerProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MCServeUpdate\\MCServeUpdate.exe";
                UpdateManagerProcess.Start();

                // Stops the current update-manager
                Environment.Exit(0);
            }

            #endregion

            #region  # Set the program environment #

            //Console.Clear();
            MCEnvironment.SetConsoleIcon(Resources.MCServe_Update);
            Console.Title = "MCServe Update Manager";
            Console.WriteLine("MCServe Update Manager\n");

            #endregion

            #region # Laod the update data #

            Console.WriteLine("Load Update-Data");
            //StreamReader jsonRawReader = new StreamReader(Settings.CurrentPath + "\\" + Settings.SettingsFile);

            #endregion


            Console.Read();
        }
    }
}
